<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Main controller
 */
class Controller_Category extends Controller_Template {
	protected $_template = 'other/master';

	/**
	 * List of categories
	 */
	public function indexAction($type_id = 0) {
		if ($type_id > 0) {
			Session::set('category_type_id', intval($type_id));
		} else if (Session::is('category_type_id')) {
			$type_id = intval(Session::get('category_type_id'));
		} else {
			$type_id = 1;
		}
		
		$this->template->content = View::factory('category/index')
			->set('type_id', $type_id)
			->set('cat_list', Category::getFullList($type_id))
			->render();
	}

	/**
	 * Insert category
	 */
	public function insertAction($error = -1) {
		if (Input::isPostBack()) {
			$cat_alias = Router::fixUrl(Input::post('cat_alias'));
			if (!empty($cat_alias)) {
				if (Category::getIdByAlias($cat_alias) > 0) {
					$cat_alias = null;
				}
			} else {
				$cat_alias = null;
			}

			$data = array();
			Query::data($data, 'name', Input::post('cat_name'), Query::DATA_STRING);
			Query::data($data, 'alias',  $cat_alias, Query::DATA_STRING);
			Query::data($data, 'type_id', Input::post('cat_type_id'), Query::DATA_INTEGER);
			Query::data($data, 'status_id', Input::post('cat_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'parent_id', Input::post('cat_parent_id_' . Input::post('cat_type_id')), Query::DATA_INTEGER);
			Query::data($data, 'insert_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'insert_time', time(), Query::DATA_INTEGER);
			
			$new_cat_id = Category::insert($data);
			if ($new_cat_id) {
				Tools::redirect(BASE_URL . 'category/update/' . $new_cat_id . '/-2');
			} else {
				Tools::redirect(CURRENT_URL . '1');
			}
		} else {
			$view = View::factory('category/insert')
				->set('error', $error);
			
			$this->template->content = $view->render();
		}
	}

	/**
	 * Update category
	 */
	public function updateAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$cat_alias = Router::fixUrl(Input::post('cat_alias'));
			if (!empty($cat_alias)) {
				if (Category::getIdByAlias($cat_alias, $id) > 0) {
					$cat_alias = null;
				}
			} else {
				$cat_alias = null;
			}

			$data = array();
			Query::data($data, 'name', Input::post('cat_name'), Query::DATA_STRING);
			Query::data($data, 'alias',  $cat_alias, Query::DATA_STRING);
			Query::data($data, 'status_id', Input::post('cat_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'parent_id', Input::post('cat_parent_id'), Query::DATA_INTEGER);
			Query::data($data, 'update_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'update_time', time(), Query::DATA_INTEGER);

			
			if (Category::update($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$view = View::factory('category/update')
				->set('error', $error)
				->set('info', Category::getInfo($id));
			
			$this->template->content = $view->render();
		}
	}

	public function deleteAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'status_id', 3, Query::DATA_INTEGER);
			Query::data($data, 'sequence', 0, Query::DATA_INTEGER);
			Query::data($data, 'delete_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'delete_time', time(), Query::DATA_INTEGER);

			if (Category::delete($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$view = View::factory('category/delete')
				->set('error', $error)
				->set('info', Category::getInfo($id));
			
			$this->template->content = $view->render();
		}
	}

	public function sequenceAction($id, $direction = 0) {
		$error = -1;
		if ($direction > 0) {
			$result = false;
			
			if (1 == $direction) {
				$result = Category::moveUp($id);
			} else if (2 == $direction) {
				$result = Category::moveDown($id);
			}
			
			if (!$result) {
				$error = 1;
			}
		}

		$cat_info = Category::getInfo($id);
		$view = View::factory('category/sequence')
			->set('error', $error)
			->set('info', $cat_info)
			->set('list', Category::getList($cat_info['parent_id'], array('type_id' => $cat_info['type_id'], 'status_id' => array(1,2))));

		$this->template->content = $view->render();
	}
}